<template>
  <div class="detail-container">
    <!-- 面包屑导航 -->
    <div class="bread container">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/lists/${cid}` }">{{ cateName }}</el-breadcrumb-item>
        <Transition name="move" mode="out-in">
          <el-breadcrumb-item :key="content.id" v-if="content">
            {{ content.title }}
          </el-breadcrumb-item>
        </Transition>
      </el-breadcrumb>
    </div>

    <div class="detail container">
      <el-row>
        <el-col :lg="18" style="opacity: 0" v-if="!content">1</el-col>
        <el-col :lg="18" class="wrap" v-if="content">
          <div class="title">{{ content.title }}</div>
          <div class="date">
            发表于{{ content.created_at.slice(0, 10) }}
            <span class="zan" @click="addLike">
              <i class="el-icon-thumb" />
              {{ zanCount }}
            </span>
          </div>
          <p><img :src="baseURL + content.image" alt="" /></p>
          <div class="content" v-html="content.content" />
          <div class="add-comment">
            <el-input type="textarea" :rows="4" placeholder="发布你的评论" v-model="textarea">
            </el-input>
            <div class="btns">
              <el-button size="small" @click="textarea = ''">清空</el-button>
              <el-button type="primary" size="small" @click="addComment">评论</el-button>
            </div>
          </div>
          <div class="comment" v-if="comCount">
            <h2 class="com-hd">{{ comCount }}条评论</h2>
            <div class="com-bd">
              <div class="row" v-for="item in comment" :key="item.id">
                <div class="user">{{ item.username }}</div>
                <div class="date">{{ item.created_at.slice(0, 10) }}</div>
                <div class="content">{{ item.content }}</div>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :lg="6">
          <MyHot />
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import MyHot from '@/components/MyHot'
import { reqContentDetail, reqAddLike, reqAddComment } from '@/api'
import { baseURL } from '@/utils/request'
import bus from '@/eventBus'

export default {
  name: 'Detail',
  components: { MyHot },
  data() {
    return {
      baseURL: baseURL + 'upload/',
      // 分类id
      cid: '',
      // 分类名称
      cateName: '',
      // 详情内容
      content: null,
      // 评论数据列表
      comment: [],
      // 评论数
      comCount: 0,
      // 点赞数
      zanCount: 0,
      // 评论框
      textarea: ''
    }
  },
  methods: {
    async getDetail() {
      // 获取当前详情id
      const { id } = this.$route.params
      const { data: res } = await reqContentDetail(id)
      this.cateName = res.body.catename
      this.cid = res.body.cid
      this.content = res.body.content
      this.comment = res.body.comment
      this.comCount = res.body.comCount
      this.zanCount = res.body.zanCount
    },
    // 点赞
    async addLike() {
      const token = localStorage.getItem('token')
      // 判断当前是否是登录状态
      if (!token) {
        this.$confirm('请先登录后再进行操作', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => bus.$emit('visible', true))
          .catch(() => {})
        return
      }

      const { id } = this.$route.params
      const { data: res } = await reqAddLike(id)
      if (res.status !== 200) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      // 重新获取数据渲染页面
      this.getDetail()
    },
    // 评论
    async addComment() {
      const token = localStorage.getItem('token')
      // 判断当前是否是登录状态
      if (!token) {
        this.$confirm('请先登录后再进行操作', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => bus.$emit('visible', true))
          .catch(() => {})
        return
      }

      const { id } = this.$route.params
      const commentInfo = {
        id,
        comment: this.textarea
      }
      const { data: res } = await reqAddComment(commentInfo)
      if (res.status !== 200) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      // 清空文本框内容
      this.textarea = ''
      // 重新获取数据
      this.getDetail()
    }
  },
  watch: {
    $route: {
      handler() {
        this.getDetail()
        window.scrollTo(0, 0)
      },
      immediate: true
    }
  }
}
</script>

<style lang="less" scoped>
.detail-container {
  min-height: 1000px;
}

.detail {
  padding: 20px 0;

  .wrap {
    padding-right: 40px;

    .title {
      font-size: 36px;
      margin-bottom: 20px;
      transition: all 0.35s;

      &:hover {
        color: @primaryColor;
      }
    }

    > .date {
      .zan {
        margin-left: 20px;
        cursor: pointer;

        &.active {
          color: @primaryColor;
        }
      }
    }

    img {
      width: 100%;
      margin-top: 40px;
    }

    .add-comment {
      margin: 40px 0;

      .btns {
        display: flex;
        justify-content: flex-end;
        margin-top: 10px;
      }
    }

    /deep/ p {
      margin-bottom: 16px;
    }

    .comment {
      .com-hd {
        font-weight: 400;
        font-size: 26px;
        margin: 30px 0;
        color: #747474;
        border-left: 5px solid @primaryColor;
        padding-left: 10px;
      }

      .com-bd {
        .row {
          padding: 10px;

          & ~ .row {
            margin-top: 10px;
            border-top: 1px solid #eee;
          }

          .user {
            font-size: 20px;
            color: #747474;
            margin-bottom: 10px;
          }

          .date {
            display: flex;
            font-size: 12px;
            color: #bbb;
            margin-bottom: 5px;
          }

          .content {
            margin-top: 10px;
          }
        }
      }
    }
  }
}

.bread {
  display: flex;
  align-items: center;
  height: 50px;
}
</style>
